import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { map } from 'rxjs/operators';

import { LastUrlService, GetLastURL } from '../../services/last-url.service';

import { OperationDataList, ShellDataList } from './mock-data';

@Component({
    selector: 'm-area-tab',
    templateUrl: './area-tab.component.html',
    styleUrls: ['./area-tab.component.scss']
})
export class AreaTabComponent implements OnInit {
    areaButtons = [
        { link: '/logs/area/config', name: '区域调整日志' },
        { link: '/logs/area/pc', name: '终端配置日志' },
        { link: '/logs/area/privilege', name: '权限定制日志' }
    ];
    operationList = OperationDataList;
    shellList = ShellDataList;

    constructor(private route: ActivatedRoute,
        private router: Router,
        private urlService: LastUrlService) { }

    areaList$ = this.route.data.pipe(map(data => data['zone_list']));
    activeAreaBtn: string;
    activeAreaID: number;
    activeOperationID: number;
    activeShellID: number;
    searchKey: string = '';

    ngOnInit() {
        this.urlService.getURL().subscribe(url => {
            switch (url) {
                case 'config':
                    this.activeAreaBtn = this.areaButtons[0].link;
                    break;
                case 'pc':
                    this.activeAreaBtn = this.areaButtons[1].link;
                    break;
                case 'privilege':
                    this.activeAreaBtn = this.areaButtons[2].link;
                    break;
            }
        });
        this.route.queryParamMap.subscribe(data => {
            this.searchKey = data.get('keywords') || '';
            this.activeAreaID = +(data.get('area_id') || 0);
            this.activeOperationID = +(data.get('operation') || 0);
            let tmp = data.get('has_shell');
            if (!tmp) {
                this.activeShellID = -1;
            } else if (tmp == "true") {
                this.activeShellID = 1;
            } else if (tmp == "false") {
                this.activeShellID = 0;
            } else {
                this.activeShellID = -1;
            }
        });
    }

    onActiveAreaID(id: number) {
        if (this.activeAreaID == id) {
            return;
        }
        this.activeAreaID = id;
        this.startSearch();
    }

    onActiveOperationID(id: number) {
        if (this.activeOperationID == id) {
            return;
        }
        this.activeOperationID = id;
        this.startSearch();
    }

    onActiveShellID(id: number) {
        if (this.activeShellID == id) {
            return;
        }
        this.activeShellID = id;
        this.startSearch();
    }

    onSearchKey(key: string, exec: boolean) {
        if (this.searchKey == key) {
            return;
        }
        this.searchKey = key;
        if (!exec) {
            return;
        }
        this.startSearch();
    }

    startSearch() {
        let params = { page: 1 };
        if (this.activeAreaID != 0) {
            params['area_id'] = this.activeAreaID;
        }
        if (this.activeOperationID != 0) {
            params['operation'] = this.activeOperationID;
        }
        if (this.activeShellID != -1) {
            params['has_shell'] = this.activeShellID ? true : false;
        }
        if (this.searchKey.length != 0) {
            params['keywords'] = this.searchKey;
        }
        this.router.navigate([GetLastURL(this.route)], {
            relativeTo: this.route,
            queryParams: params
        });
    }
}
